package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.ShowLargeImageActivity;
import com.hemaapp.yjnh.bean.Blog;
import com.hemaapp.yjnh.bean.Img;
import com.hemaapp.yjnh.bean.ImgItemsBean;
import com.hemaapp.yjnh.bean.Reply;
import com.hemaapp.yjnh.listener.OnJournalClickListener;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.view.ImageGridView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：田家农货的详情适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/15 10:12
 * 修改备注：
 */
public class FarmerCropDetailAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ImgItemsBean> images;
    private ArrayList<Blog> journals;
    private ArrayList<Reply> replies;
    private OnJournalClickListener itemClickListener;
    private int type = 1;//1商品详情，2评论, 3日志
    private boolean isOwner = false;

    public FarmerCropDetailAdapter(Context context, ArrayList<ImgItemsBean> images,
                                   ArrayList<Blog> journals, ArrayList<Reply> replies, int type) {
        super(context);
        this.mContext = context;
        this.images = images;
        this.journals = journals;
        this.replies = replies;
        this.type = type;
    }

    public void setOwner(boolean owner) {
        isOwner = owner;
    }

    public void setItemClickListener(OnJournalClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setImages(ArrayList<ImgItemsBean> images) {
        this.images = images;
    }

    public void setReplies(ArrayList<Reply> replies) {
        this.replies = replies;
    }

    public void setJournals(ArrayList<Blog> journals) {
        this.journals = journals;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        if (type == 1)
//            return images.size() == 0 ? 1 : images.size();
            return 1;
        else if (type == 3)
            return journals.size() == 0 ? 1 : journals.size();
        else if (type == 2)
            return replies.size() == 0 ? 1 : replies.size();
        else
            return 0;
    }

    @Override
    public boolean isEmpty() {
        int size = 0;
        if (type == 1)
            size = images == null ? 0 : images.size();
        else if (type == 3)
            size = journals == null ? 0 : journals.size();
        else if (type == 2)
            size = replies == null ? 0 : replies.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        if (type == 1) {
            ViewHolder1 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_DETAIL) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_custom_detail_image, parent, false);
                holder = new ViewHolder1(convertView);
                convertView.setTag(R.id.TAG_DETAIL, holder);
            } else
                holder = (ViewHolder1) convertView.getTag(R.id.TAG_DETAIL);
            holder.setDatas(position);
        } else if (type == 3) {
            ViewHolder3 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_JOURNAL) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_journal, parent, false);
                holder = new ViewHolder3(convertView);
                convertView.setTag(R.id.TAG_JOURNAL, holder);
            } else
                holder = (ViewHolder3) convertView.getTag(R.id.TAG_JOURNAL);
            holder.setDatas(position);
        } else if (type == 2) {
            ViewHolder2 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_REPLY) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_production_reply, parent, false);
                holder = new ViewHolder2(convertView);
                convertView.setTag(R.id.TAG_REPLY, holder);
            } else
                holder = (ViewHolder2) convertView.getTag(R.id.TAG_REPLY);
            holder.setDatas(position);
        }
        return convertView;
    }

    class ViewHolder1 {
//        @Bind(R.id.iv_detail)
//        ImageView ivDetail;
        @Bind(R.id.webview)
        WebView webview;
        int position;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
            BaseUtil.initWebview(webview);
//            ivDetail.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    ArrayList<Img> imgs = new ArrayList<Img>();
//                    for (int i = 0; i < images.size(); i++) {
//                        ImgItemsBean item = images.get(i);
//                        String imgurl_detail = item.getImgurlbig();
//                        Img img = new Img(imgurl_detail);
//                        imgs.add(img);
//                    }
//
//                    Intent intent = new Intent();
//                    intent.setClass(mContext, ShowLargeImageActivity.class);
//                    intent.putExtra("imgs", imgs);
//                    intent.putExtra("position", position + 1);
//                    mContext.startActivity(intent);
//                }
//            });
        }

        private void setDatas(int position) {
            this.position = position;
//            ImageUtils.loadBigImage(mContext, images.get(position).getImgurlbig(), ivDetail);
            StringBuilder sb = new StringBuilder();
            sb.append("<html>\n" +
                    "\t<head>\n" +
                    "\t\t<meta charset=\"utf-8\" />\n" +
                    "\t\t<title></title>\n" +
                    "\t</head>\n" +
                    "\t<body>\n");
            for (ImgItemsBean image : images) {
                sb.append("<img src=\"");
                sb.append(image.getImgurlbig());
                sb.append("\" width=\"100%\"/>\n" +
                        "\t\t<br />");
            }
            sb.append("\t</body>\n" +
                    "</html>");
            webview.loadDataWithBaseURL(null,sb.toString(), "text/html", "utf-8", null);
        }
    }

    class ViewHolder2 {
        @Bind(R.id.iv_avatar)
        ImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.iv_reply)
        ImageView ivReply;
        @Bind(R.id.tv_reply)
        TextView tvReply;
        @Bind(R.id.image_grid)
        ImageGridView imageGrid;
        @Bind(R.id.tv_merchant_date)
        TextView tvMerchantDate;
        @Bind(R.id.tv_merchant_reply)
        TextView tvMerchantReply;
        @Bind(R.id.layout_merchant_reply)
        LinearLayout layoutMerchantReply;
        @Bind(R.id.line_merchant_reply)
        View lineMerchantReply;
        int position = -1;
        AddImageAdapter imageAdapter;
        ArrayList<ImgItemsBean> replyImgs = new ArrayList<>();

        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.OnItemClickListener(position);
                }
            });
            ivReply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.OnCommentClickListener(position);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Reply reply = replies.get(position);
            ImageUtils.loadAvatar(mContext, reply.getAvatar(), ivAvatar);
            tvName.setText(reply.getNickname());
            tvDate.setText(HemaUtil.transTimeChat(reply.getRegdate()));
            tvReply.setText(reply.getContent());
            replyImgs.clear();
            replyImgs.addAll(reply.getItems());
            refreshImages();
            //显示商家回复
            String sellerflag = reply.getSellerflag();// 商家回复标志	0否；1是
            if (sellerflag.equals("1")) {
                layoutMerchantReply.setVisibility(View.VISIBLE);
                Reply.ReplyChild child = reply.getChilds().get(0);
                tvMerchantDate.setText(HemaUtil.transTimeChat(child.getRegdate()));
                tvMerchantReply.setText(child.getContent());
            }else {
                layoutMerchantReply.setVisibility(View.GONE);
            }
            if (isOwner && sellerflag.equals("0")) {//是自己的商品，而且没有回复
                ivReply.setVisibility(View.VISIBLE);
            }else
                ivReply.setVisibility(View.GONE);
        }

        private void refreshImages() {
            if (imageAdapter == null) {
                imageAdapter = new AddImageAdapter(mContext, replyImgs);
                imageGrid.setAdapter(imageAdapter);
                imageAdapter.setShowDelete(false);
                imageAdapter.setShowAdd(false);
                imageAdapter.setImageClickListener(new AddImageAdapter.OnImageClickListener() {
                    @Override
                    public void onImageClickListener(int position) {
                        ArrayList<Img> imgs = new ArrayList<Img>();
                        for (int i = 0; i < replyImgs.size(); i++) {
                            ImgItemsBean item = replyImgs.get(i);
                            String imgurl_detail = item.getImgurl();
                            Img img = new Img(imgurl_detail);
                            imgs.add(img);
                        }
                        Intent intent = new Intent();
                        intent.setClass(mContext, ShowLargeImageActivity.class);
                        intent.putExtra("imgs", imgs);
                        intent.putExtra("position", position + 1);
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void onDeleteClickListener(int position, ImgItemsBean imageItem) {
                    }
                });
            } else {
                imageAdapter.notifyDataSetChanged();
            }
        }
    }

    class ViewHolder3 {
        @Bind(R.id.tv_release_date)
        TextView tvReleaseDate;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.image_grid)
        ImageGridView imageGrid;
        @Bind(R.id.tv_zan_count)
        TextView tvZanCount;
        @Bind(R.id.layout_zan)
        LinearLayout layoutZan;
        @Bind(R.id.tv_comment_count)
        TextView tvCommentCount;
        @Bind(R.id.layout_comment)
        LinearLayout layoutComment;
        int position = -1;
        AddImageAdapter imageAdapter;
        ArrayList<ImgItemsBean> additonImg = new ArrayList<>();

        ViewHolder3(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.OnItemClickListener(position);
                }
            });
            tvCommentCount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.OnCommentClickListener(position);
                }
            });
            tvZanCount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null && position != -1)
                        itemClickListener.OnZanClickListener(position);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Blog blog = journals.get(position);
            tvReleaseDate.setText(HemaUtil.transTimeChat(blog.getRegdate())+"    发布");
            tvContent.setText(blog.getContent());
            tvZanCount.setText(blog.getGoodcount());
            if (blog.getGoodflag().equals("1")) {// 点赞标记	0无 1有
                tvZanCount.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getResources().getDrawable(R.drawable.thumbs_up_orange), null, null, null);
            }else {
                tvZanCount.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getResources().getDrawable(R.drawable.thumbs_up_grey), null, null, null);
            }
            tvCommentCount.setText(journals.get(position).getReplycount());
            additonImg.clear();
            additonImg.addAll(journals.get(position).getImgItems());
            refreshImages();
        }

        private void refreshImages() {
            if (imageAdapter == null) {
                imageAdapter = new AddImageAdapter(mContext, additonImg);
                imageGrid.setAdapter(imageAdapter);
                imageAdapter.setShowDelete(false);
                imageAdapter.setShowAdd(false);
                imageAdapter.setImageClickListener(new AddImageAdapter.OnImageClickListener() {
                    @Override
                    public void onImageClickListener(int position) {
                        ArrayList<Img> imgs = new ArrayList<Img>();
                        for (int i = 0; i < additonImg.size(); i++) {
                            ImgItemsBean item = additonImg.get(i);
                            String imgurl_detail = item.getImgurlbig();
                            Img img = new Img(imgurl_detail);
                            imgs.add(img);
                        }

                        Intent intent = new Intent();
                        intent.setClass(mContext, ShowLargeImageActivity.class);
                        intent.putExtra("imgs", imgs);
                        intent.putExtra("position", position + 1);
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void onDeleteClickListener(int position, ImgItemsBean imageItem) {
                    }
                });
            } else {
                imageAdapter.notifyDataSetChanged();
            }
        }
    }
}
